//
//  FYNavigationViewController.m
//  MockProject
//
//  Created by FanYu on 8/20/16.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXNavigationController.h"

@interface YXNavigationController () <UINavigationControllerDelegate, UIGestureRecognizerDelegate>

@end

@implementation YXNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 修改返回后 重新继承
    __weak typeof(self) weakSelf = self;
    if ([self respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.interactivePopGestureRecognizer.delegate = weakSelf;
    }
    [self setupNavigationBar];
}


#pragma mark - Setup UI 
// 设置navigationBar
- (void)setupNavigationBar {
    
    UINavigationBar *appearance = [UINavigationBar appearance];
    
    // title
    NSDictionary *textAttrs = @{
                                NSForegroundColorAttributeName : [UIColor whiteColor],
                                NSFontAttributeName            : [UIFont systemFontOfSize:18]
                               };
    [appearance setTitleTextAttributes:textAttrs];
    
    // bar tint color
    appearance.barTintColor = [UIColor colorWithHexString:@"ff7000"];

    // back button color
    //appearance.tintColor = [UIColor whiteColor];
    
    // back chevron
    // appearance.backIndicatorImage = [UIImage imageNamed:@"返回按钮正常态"];
}

// Back button action status
- (void)setupNavigationItem {
    UIBarButtonItem *appearance = [UIBarButtonItem appearance];
    
    // 设置普通状态文字属性
    NSMutableDictionary *textAtrs = [NSMutableDictionary dictionary];
    textAtrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    textAtrs[NSFontAttributeName] = [UIFont systemFontOfSize:15];
    [appearance setTitleTextAttributes:textAtrs forState:UIControlStateNormal];
    
    // 设置高亮状态文字属性
    NSMutableDictionary *textHighlighted = [NSMutableDictionary dictionary];
    textHighlighted[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    [appearance setTitleTextAttributes:textHighlighted forState:UIControlStateHighlighted];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        // 设置导航栏按钮
        self.backButton = [[UIButton alloc] init];
        self.backButton.frame = CGRectMake(0, 0, 30, 30);
        [self.backButton setContentEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 15)];
        [self.backButton setImage:[UIImage imageNamed:@"返回按钮正常态"] forState:UIControlStateNormal];
        [self.backButton setImage:[UIImage imageNamed:@"返回按钮-点击态"] forState:UIControlStateHighlighted];
        [self.backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.backButton];
    }
    [super pushViewController:viewController animated:animated];
}

- (void)back {
    [self popViewControllerAnimated:YES];
}

#pragma mark - <UIGestureRecognizerDelegate>
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
    if ((self.viewControllers.count < 2) || (self.visibleViewController == [self.viewControllers objectAtIndex:0])) {
        return NO;
    }
    return YES;
}


#pragma mark - Public 
- (void)changeToWhiteBar {
    self.navigationBar.barTintColor = [UIColor colorWithHexString:@"fcfcfc"];
    
    NSDictionary *textAttrs = @{
                                NSForegroundColorAttributeName : [UIColor colorWithHexString:@"403e3d"],
                                NSFontAttributeName            : [UIFont systemFontOfSize:18]
                                };
    self.navigationBar.titleTextAttributes = textAttrs;
    
    [self.backButton setImage:[UIImage imageNamed:@"设置返回按钮-正常态"] forState:UIControlStateNormal];
    [self.backButton setImage:[UIImage imageNamed:@"设置返回按钮-点击态"] forState:UIControlStateHighlighted];
}

- (void)changeToDefaultBar {
    self.navigationBar.barTintColor = [UIColor colorWithHexString:@"ff7000"];
    
    NSDictionary *textAttrs = @{
                                NSForegroundColorAttributeName : [UIColor whiteColor],
                                NSFontAttributeName            : [UIFont systemFontOfSize:18]
                                };
    self.navigationBar.titleTextAttributes = textAttrs;
    
    [self.backButton setImage:[UIImage imageNamed:@"返回按钮正常态"] forState:UIControlStateNormal];
    [self.backButton setImage:[UIImage imageNamed:@"返回按钮-点击态"] forState:UIControlStateHighlighted];
}
@end
